<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Config;
use think\Session;
class Login extends Controller
{
	function _initialize(){
		//开启session
		session_start();
	}
	/*function index1(){
		$QR=$image_url= ROOT_PATH . 'public' . DS . 'qrcode/a.png';
		$logo=ROOT_PATH . 'public' . DS . 'qrcode/logo.png';
		vendor('phpqrcode.phpqrcode');//引入插件类
		$url = 'http://ww.baidu.com';         //二维码内容
		$errorCorrectionLevel = 'L';  //容错级别
		$matrixPointSize = 10;      //生成图片大小
		//生成二维码图片
		$QR = \QRcode::png($url,false,$errorCorrectionLevel, $matrixPointSize,2);exit;//直接返回图片，不生成图片
		\QRcode::png($url,$image_url,$errorCorrectionLevel, $matrixPointSize,2);
		if($logo !== FALSE){ 
			$QR = imagecreatefromstring(file_get_contents($QR)); 
			$logo = imagecreatefromstring(file_get_contents($logo)); 
			$QR_width = imagesx($QR); 
			$QR_height = imagesy($QR); 
			$logo_width = imagesx($logo); 
			$logo_height = imagesy($logo); 
			$logo_qr_width = $QR_width / 5; 
			$scale = $logo_width / $logo_qr_width; 
			$logo_qr_height = $logo_height / $scale; 
			$from_width = ($QR_width - $logo_qr_width) / 2; 
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); 
			 // 生成最终的文件
		}
		imagepng($QR);exit;
	}*/
	//登录页面
    function index(){
		/*$cu_da = array(
			'xingming'=>'马建新',//姓名
			'dianhua'=>'1570212121515',//电话
			'shenfenzheng'=>'513125199212125121',//身份证
			'shenqingjine'=>500,//申请金额
			'daozhangjine'=>500,//到账金额
			'shenqingshijian'=>date("Y-m-d H:i:s"),//申请时间
			'daozhangka'=>'5641654654162158',//到账卡
			'daozhangyinhang'=>'没有银行',//到账银行名称
			'shangji'=>'张三,李四',//上级
			'chongzhizonge'=>'200',//充值总额
			'shouyizonge'=>'300',//收益总额
		);
		dump($cu_da);
		dump(curl_http_post(Config::get('wxtz_url'),$cu_da));exit;*/
		// echo date('Y-m-d H:i:s',strtotime(date('Y-m-1',strtotime('+2 month'))));die;
		// return $this->fetch('index1');die;
		// echo '<img src="'.Url('admin/Login/index1').'">';die;
		$uid=session('uid');//用户id
		if($uid>0){
			//已登录
			$this->redirect('admin/index/index');die;
			//跳转主页面
		}
		return $this->fetch();die;
		//加载登录界面
	}
	//登陆操作
    function login_in(){		
        if ($this->request->isAjax()) {
			//ajax提交
			$verify=$this->request->param('verify');
			//获取验证码
			$captcha = new \think\captcha\Captcha();
			if(!$captcha->check($verify)){
				//验证码出错
				return json('-2');die;
			}
			$username=$this->request->param('username');//用户名
			$password=$this->request->param('password');//密码
			$check_name=check_format($username,Config::get('username'));
			//验证用户格式
			if(!$check_name){
				//用户名格式错误
				return json('1');die;
			}
			$check_pwd=check_format($password,Config::get('password'));
			//验证密码格式
			if(!$check_pwd){
				//密码格式错误
				return json('2');die;
			}
			$password=md5(md5($password));//用户名加密
			$user=Db::name("staff a")
				->where(array('a.name'=>$username,'a.password'=>$password,'a.is_enable'=>'1'))
				->find();
			//查询用户
			if(empty($user)){
				//用户名或密码出错
				return json('-3');die;
			}else{
				//有用户
				$uid=$user['sid'];
				$ip = $_SERVER['REMOTE_ADDR'];//登录ip
				Db::name("staff")->where(array('sid'=>$uid))->update(array('login_ip'=>$ip,'login_time'=>time()));
				//用户最后登录的ip和时间修改
				session('uid',$uid);
				return json('-1');die;
				//登录成功
			}
        }else{
			//非ajax提交
           $this->redirect('Admin/Login/index');
		}
    }
	//退出
	function login_out(){
		session(null);
		$this->redirect('admin/Login/index');die;
	}
}
